<template>
  <div id="code" >
    <el-card>
      <el-row>
        <el-col :span="24">
          <div class="grid-content">
            <span style="font-size: 14px;color: #909399;font-weight: 600">作用域：</span>
            <el-select v-model="pageSearch.clauses[1].value"
                       @change="selectChange"
                       clearable placeholder="请选择">
              <el-option
                      v-for="item in options"
                      :key="item.id"
                      :label="item.codeDesc"
                      :value="item.id">
              </el-option>
            </el-select>
            <el-button style="float: right;" size="medium " type="primary" @click="addCode(false)">新增作用域</el-button>
            <el-button style="float: right;margin-right: 10px" size="medium " type="primary" @click="addCode(true)">新增码表值</el-button>
          </div>
        </el-col>
      </el-row>
      <CodeList :data="codeData"
                @codeNeedEdit="showCodeEdit"
                @deleteCode="deleteCode"
                style="margin-top: 10px" >
      </CodeList>
      <el-pagination
              layout="prev, pager, next"
              @current-change="handleCurrentChange"
              :total="total">
      </el-pagination>
      <CodeEdit
              :dialogFormVisible="dialogFormVisible"
              :data="codeDetail"
              :title="title"
              :type="type"
              @btnAddOk="saveForm"
              @btnUpdateOk="updateForm"
              @cancel="cancel"
      >
      </CodeEdit>
    </el-card>
  </div>
</template>

<script>
  import CodeList from './codeList'
  import CodeEdit from './codeEdit'
  import Message from '../../../components/messages/index'
  export default {
    data(){
      return {
        dialogFormVisible:false,
        options: [],
        codeData:[],
        total:0,
        pageSearch:{
          pageSize:10,
          pageNum:1,
          clauses:[{
            column:"parentId",
            operation:"isNotNull",
            value:"isNotNull",
          }, {
            column:"parentId",
            operation:"=",
            value:"",
          }],
          sortMap:{}
        },
        codeDetail:[],
        title:"",
        type:Boolean
      }
    },
    methods:{
      showCodeEdit(val){
        this.title="修改码表值";
        this.type=true;
        this.codeDetail=[val,this.options];
        this.dialogFormVisible=true
      },
      addCode(val){
        if(val){
          this.title="新增码表值";
          this.type=true;
        }else{
          this.title="新增作用域";
          this.type=false;
        }
        this.codeDetail=[null,this.options];
        this.dialogFormVisible=true;
      },
      deleteCode(id){
        Message.confirm2("确定永久删除数据？","warning").then(()=>{
          this.$http.get("/coder/delete/"+id).then(res=>{
            if(res.status===200){
              Message.success("删除成功");
              this.getList();
            }
          })
        })
      },
      saveForm(val){
        let self=this;
        self.$http.post("coder/add",val).then(res=>{
          if (res.status===200){
            Message.success(this.title+"成功");
            this.getList();
            this.getClassName();
          }
        });
        self.dialogFormVisible=false;

      },
      updateForm(val){
        let self=this;
        self.$http.post("coder/update",val).then(res=>{
          if (res.status===200){
            Message.success(this.title+"成功");
            this.getList();
          }
        });
        self.dialogFormVisible=false;

      },
      cancel(){
        this.dialogFormVisible = false;
      },
      selectChange(){
        this.pageSearch.pageSize=10;
        this.pageSearch.pageNum=1;
        this.getList();
      },
      getClassName(){
        this.$http.post("/coder/selectClassName").then(res=> {
          if(res.status==200){
            this.options=res.data;
          }
        })
      },
      getList(){
        let self=this;
        self.$http.post("/coder/page",this.pageSearch).then(res=>{
          if(res.status==200){
            this.codeData=res.data.list;
            this.total=res.data.total;
          }
        })
      },
      handleCurrentChange(val){
        this.pageSearch.pageNum=val;
        this.getList();
      },

    },
    mounted(){
      this.getClassName();
      this.getList();
    },
    components: {CodeList,CodeEdit},
  }
</script>

<style scoped>
  .grid-content{
    height: 40px;
  }
</style>

